define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'integral/taskbook/index',
                    add_url: 'integral/taskbook/add/ids/'+week_ids+'/task_id/'+task_ids,
                    edit_url: 'integral/taskbook/edit',
                    del_url: 'integral/taskbook/delete',
                    addschool_url: 'integral/taskbook/addschool',
                    multi_url: 'integral/taskbook/multi',
                    review_url: 'tasks/task/review/task_id/'+task_ids,
                    check_url: 'tasks/task/check/type/2/task_id/'+task_ids,
                    table: 'taskbook',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'status_text', title: __('Task.Status'),
                          table: table,
                          events: Controller.api.events.StatusDesc,
                          buttons: [],
                          formatter: Controller.api.formatter.StatusDesc
                        },
                        {field: 'school_text', title: __('School.School_id')},
                        {field: 'createtime', title: __("Createtime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __("Updatetime"), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,buttons: [],formatter:Controller.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                  //这里可以追加搜索条件
                  var filter = JSON.parse(params.filter);
                  var op = JSON.parse(params.op);
                  //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                  filter.task_id = task_ids;
                  filter.delete_status = 1;
                  op.week_id = "=";
                  op.delete_status = "=";
                  params.filter = JSON.stringify(filter);
                  params.op = JSON.stringify(op);
                  return params;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('.btn-add-school').on('click', function () {
              var that = this;
              Fast.api.open('integral/taskbook/addschool', '添加新的学校', $(that).data() || {});
            });
          Controller.api.bindevent();
        },
        edit: function () {
            $('.btn-add-school').on('click', function () {
              var that = this;
              Fast.api.open('integral/taskbook/addschool', '添加新的学校', $(that).data() || {});
            });
            Controller.api.bindevent();
        },
        addschool: function(){
          Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                StatusDesc: function (value, row, index) {
                      
                    return '<a class="btn btn-xs btn-ip"> '+value+ '</a>';
                },
               operate: function (value, row, index) {
                    var table = this.table;
                    // 操作配置
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    var check = 0;
                    var review = 0;
                    switch (true) {
                        case row.status == 1 || row.status == 2:
                            check = 1;
                          break;
                        // case row.status == 3:
                        //     review = 1;
                        //     break;    
                        // case row.status == 4:
                        //     review = 2;
                        //     break;
                        // case row.status == 5 || row.status == 8:
                        //     review = 3; 
                        //     break;
                        // case row.status == 6:
                        //     review = 4;
                        //     break;
                        // case row.status == 7:
                        //     review = 5;
                        //     break;
                        default:
                           break;
                    }
                    if(check){
                          buttons.push({
                            name: 'check',
                            icon: 'fa fa-paper-plane',
                            title: __('Check'),
                            extend: 'data-toggle="tooltip"',
                            classname: 'btn btn-xs btn-success btn-checkone',
                            url: options.extend.check_url
                          });
                        buttons.push({
                              name: 'edit',
                              icon: 'fa fa-pencil',
                              title: __('Edit'),
                              extend: 'data-toggle="tooltip" data-area=\'["80%","80%"]\'',
                              classname: 'btn btn-xs btn-success btn-editone',
                          url: options.extend.edit_url
                        });
                        buttons.push({
                          name: 'del',
                          icon: 'fa fa-trash',
                          title: __('Del'),
                          extend: 'data-toggle="tooltip"',
                          classname: 'btn btn-xs btn-danger btn-delone',
                          url: options.extend.del_url
                        });
                    }
                    if(review){
                         buttons.push({
                            name: 'review'+review,
                            icon: 'fa fa-sticky-note',
                            title: __('SubStatus'),
                            classname: 'btn btn-xs btn-warning btn-dialog',
                            extend: 'data-toggle="tooltip" data-area=\'["60%","80%"]\'',
                            url: options.extend.review_url+'/status/'+review
                          });
                    }
                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                },
               },
             events: {//绑定事件的方法

                StatusDesc: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                           var content_data ='<table  class="table table-striped table-bordered table-hover table-nowrap" data-operate-edit="1" data-operate-del="1" width="100%"><thead><tr> <th style="text-align: center;">'+__('Task.Date')+'</th><th style="text-align: center;">'+__('Task.Status')+'</th><th style="text-align: center;">'+__('Task.Check')+'</th><th style="text-align: center;">'+__('Task.Publish')+'</th><th style="text-align: center;">'+__('Status_desc')+'</th></tr></thead><tbody id="ultable" >';
                             content_data += row.status_desc+'</tbody></table>';
                             layer.open({
                               type: 1,
                               skin: 'layui-layer-demo', //样式类名
                               title:'审核记录',
                               //closeBtn: 0, //不显示关闭按钮
                               anim: 2,
                               shadeClose: true, //开启遮罩关闭
                               area: ['50%', '50%'],
                               
                               content: content_data
                            });
                    }
                }
            },
          },
       
     };
    return Controller;
});